package com.example.sport.ui.dashboard;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.sport.R;
import com.example.sport.ui.dashboard.recyclercalendar.CalendarAdapter;
import com.example.sport.ui.dashboard.recyclercalendar.Day;

import java.util.ArrayList;
import java.util.List;

public class DashboardFragment extends Fragment {
    private CalendarAdapter adapter;
    private RecyclerView recyclerView;
    private List<Day> days;
    private Context mContext;
    boolean flag1 = false;boolean flag2=false;boolean flag3=false;boolean flag4=false;
    boolean flag5=false;boolean flag6=false;boolean flag7=false;boolean flag8=false;
    ImageView water1 ;ImageView water5 ;
    ImageView water2 ;ImageView water6 ;
    ImageView water3 ;ImageView water7 ;
    ImageView water4 ;ImageView water8 ;
    private EditText waterNum;
    private PopupWindow mPopupWindow;
    private PopupWindow mPopupWindow1;
    @RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
    @SuppressLint("ResourceType")
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_dashboard, container, false);

        ImageView water1 = inflate.findViewById(R.id.iv4_1);
        ImageView water2 = inflate.findViewById(R.id.iv4_2);
        ImageView water3 = inflate.findViewById(R.id.iv4_3);
        ImageView water4 = inflate.findViewById(R.id.iv4_4);
        ImageView water5 = inflate.findViewById(R.id.iv4_5);
        ImageView water6 = inflate.findViewById(R.id.iv4_6);
        ImageView water7 = inflate.findViewById(R.id.iv4_7);
        ImageView water8 = inflate.findViewById(R.id.iv4_8);
        water1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                if(!flag1){
                    water1.setImageResource(R.drawable.pic_4_water2);
                    flag1=true;
                }else{
                    water1.setImageResource(R.drawable.pic_4_water1);
                    flag1=false;
                    water2.setImageResource(R.drawable.pic_4_water1);
                    flag2=false;
                    water3.setImageResource(R.drawable.pic_4_water1);
                    flag3=false;
                    water4.setImageResource(R.drawable.pic_4_water1);
                    flag4=false;
                    water5.setImageResource(R.drawable.pic_4_water1);
                    flag5=false;
                    water6.setImageResource(R.drawable.pic_4_water1);
                    flag6=false;
                    water7.setImageResource(R.drawable.pic_4_water1);
                    flag7=false;
                    water8.setImageResource(R.drawable.pic_4_water1);
                    flag8=false;
                }
            }
        });

        water2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!flag2){
                    water2.setImageResource(R.drawable.pic_4_water2);
                    flag2=true;
                    water1.setImageResource(R.drawable.pic_4_water2);
                    flag1=true;
                }else{
                    water1.setImageResource(R.drawable.pic_4_water1);
                    flag1=false;
                    water2.setImageResource(R.drawable.pic_4_water1);
                    flag2=false;
                    water3.setImageResource(R.drawable.pic_4_water1);
                    flag3=false;
                    water4.setImageResource(R.drawable.pic_4_water1);
                    flag4=false;
                    water5.setImageResource(R.drawable.pic_4_water1);
                    flag5=false;
                    water6.setImageResource(R.drawable.pic_4_water1);
                    flag6=false;
                    water7.setImageResource(R.drawable.pic_4_water1);
                    flag7=false;
                    water8.setImageResource(R.drawable.pic_4_water1);
                    flag8=false;
                }
            }
        });

        water3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!flag3){
                    water3.setImageResource(R.drawable.pic_4_water2);
                    flag3=true;
                    water2.setImageResource(R.drawable.pic_4_water2);
                    flag2=true;
                    water1.setImageResource(R.drawable.pic_4_water2);
                    flag1=true;
                }else{
                    water1.setImageResource(R.drawable.pic_4_water1);
                    flag1=false;
                    water2.setImageResource(R.drawable.pic_4_water1);
                    flag2=false;
                    water3.setImageResource(R.drawable.pic_4_water1);
                    flag3=false;
                    water4.setImageResource(R.drawable.pic_4_water1);
                    flag4=false;
                    water5.setImageResource(R.drawable.pic_4_water1);
                    flag5=false;
                    water6.setImageResource(R.drawable.pic_4_water1);
                    flag6=false;
                    water7.setImageResource(R.drawable.pic_4_water1);
                    flag7=false;
                    water8.setImageResource(R.drawable.pic_4_water1);
                    flag8=false;
                }
            }
        });

        water4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!flag4){
                    water4.setImageResource(R.drawable.pic_4_water2);
                    flag4=true;
                    water3.setImageResource(R.drawable.pic_4_water2);
                    flag3=true;
                    water2.setImageResource(R.drawable.pic_4_water2);
                    flag2=true;
                    water1.setImageResource(R.drawable.pic_4_water2);
                    flag1=true;
                }else{
                    water1.setImageResource(R.drawable.pic_4_water1);
                    flag1=false;
                    water2.setImageResource(R.drawable.pic_4_water1);
                    flag2=false;
                    water3.setImageResource(R.drawable.pic_4_water1);
                    flag3=false;
                    water4.setImageResource(R.drawable.pic_4_water1);
                    flag4=false;
                    water5.setImageResource(R.drawable.pic_4_water1);
                    flag5=false;
                    water6.setImageResource(R.drawable.pic_4_water1);
                    flag6=false;
                    water7.setImageResource(R.drawable.pic_4_water1);
                    flag7=false;
                    water8.setImageResource(R.drawable.pic_4_water1);
                    flag8=false;
                }
            }
        });

        water5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!flag5){
                    water5.setImageResource(R.drawable.pic_4_water2);
                    flag5=true;
                    water4.setImageResource(R.drawable.pic_4_water2);
                    flag4=true;
                    water3.setImageResource(R.drawable.pic_4_water2);
                    flag3=true;
                    water2.setImageResource(R.drawable.pic_4_water2);
                    flag2=true;
                    water1.setImageResource(R.drawable.pic_4_water2);
                    flag1=true;
                }else{
                    water1.setImageResource(R.drawable.pic_4_water1);
                    flag1=false;
                    water2.setImageResource(R.drawable.pic_4_water1);
                    flag2=false;
                    water3.setImageResource(R.drawable.pic_4_water1);
                    flag3=false;
                    water4.setImageResource(R.drawable.pic_4_water1);
                    flag4=false;
                    water5.setImageResource(R.drawable.pic_4_water1);
                    flag5=false;
                    water6.setImageResource(R.drawable.pic_4_water1);
                    flag6=false;
                    water7.setImageResource(R.drawable.pic_4_water1);
                    flag7=false;
                    water8.setImageResource(R.drawable.pic_4_water1);
                    flag8=false;
                }
            }
        });

        water6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!flag6){
                    water6.setImageResource(R.drawable.pic_4_water2);
                    flag6=true;
                    water5.setImageResource(R.drawable.pic_4_water2);
                    flag5=true;
                    water4.setImageResource(R.drawable.pic_4_water2);
                    flag4=true;
                    water3.setImageResource(R.drawable.pic_4_water2);
                    flag3=true;
                    water2.setImageResource(R.drawable.pic_4_water2);
                    flag2=true;
                    water1.setImageResource(R.drawable.pic_4_water2);
                    flag1=true;
                }else{
                    water1.setImageResource(R.drawable.pic_4_water1);
                    flag1=false;
                    water2.setImageResource(R.drawable.pic_4_water1);
                    flag2=false;
                    water3.setImageResource(R.drawable.pic_4_water1);
                    flag3=false;
                    water4.setImageResource(R.drawable.pic_4_water1);
                    flag4=false;
                    water5.setImageResource(R.drawable.pic_4_water1);
                    flag5=false;
                    water6.setImageResource(R.drawable.pic_4_water1);
                    flag6=false;
                    water7.setImageResource(R.drawable.pic_4_water1);
                    flag7=false;
                    water8.setImageResource(R.drawable.pic_4_water1);
                    flag8=false;
                }
            }
        });

        water7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!flag7){
                    water7.setImageResource(R.drawable.pic_4_water2);
                    flag7=true;
                    water6.setImageResource(R.drawable.pic_4_water2);
                    flag6=true;
                    water5.setImageResource(R.drawable.pic_4_water2);
                    flag5=true;
                    water4.setImageResource(R.drawable.pic_4_water2);
                    flag4=true;
                    water3.setImageResource(R.drawable.pic_4_water2);
                    flag3=true;
                    water2.setImageResource(R.drawable.pic_4_water2);
                    flag2=true;
                    water1.setImageResource(R.drawable.pic_4_water2);
                    flag1=true;
                }else{
                    water1.setImageResource(R.drawable.pic_4_water1);
                    flag1=false;
                    water2.setImageResource(R.drawable.pic_4_water1);
                    flag2=false;
                    water3.setImageResource(R.drawable.pic_4_water1);
                    flag3=false;
                    water4.setImageResource(R.drawable.pic_4_water1);
                    flag4=false;
                    water5.setImageResource(R.drawable.pic_4_water1);
                    flag5=false;
                    water6.setImageResource(R.drawable.pic_4_water1);
                    flag6=false;
                    water7.setImageResource(R.drawable.pic_4_water1);
                    flag7=false;
                    water8.setImageResource(R.drawable.pic_4_water1);
                    flag8=false;
                }
            }
        });
        water8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!flag8){
                    water8.setImageResource(R.drawable.pic_4_water2);
                    flag8=true;
                    water7.setImageResource(R.drawable.pic_4_water2);
                    flag7=true;
                    water6.setImageResource(R.drawable.pic_4_water2);
                    flag6=true;
                    water5.setImageResource(R.drawable.pic_4_water2);
                    flag5=true;
                    water4.setImageResource(R.drawable.pic_4_water2);
                    flag4=true;
                    water3.setImageResource(R.drawable.pic_4_water2);
                    flag3=true;
                    water2.setImageResource(R.drawable.pic_4_water2);
                    flag2=true;
                    water1.setImageResource(R.drawable.pic_4_water2);
                    flag1=true;
                }else{
                    water1.setImageResource(R.drawable.pic_4_water1);
                    flag1=false;
                    water2.setImageResource(R.drawable.pic_4_water1);
                    flag2=false;
                    water3.setImageResource(R.drawable.pic_4_water1);
                    flag3=false;
                    water4.setImageResource(R.drawable.pic_4_water1);
                    flag4=false;
                    water5.setImageResource(R.drawable.pic_4_water1);
                    flag5=false;
                    water6.setImageResource(R.drawable.pic_4_water1);
                    flag6=false;
                    water7.setImageResource(R.drawable.pic_4_water1);
                    flag7=false;
                    water8.setImageResource(R.drawable.pic_4_water1);
                    flag8=false;
                }
            }
        });

        View popupView = getLayoutInflater().inflate(R.layout.popup_view, null);

        mPopupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));

        Button btn1=inflate.findViewById(R.id.bn_4_button);
        btn1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mPopupWindow.showAtLocation(inflate.findViewById(R.id.bn_4_button), Gravity.BOTTOM,0,0);
                mPopupWindow.showAsDropDown(v);

            }
        });
        waterNum = popupView.findViewById(R.id.water_number);
        TextView waterSumView = inflate.findViewById(R.id.tv4_2);
        Button confirmWaterPlan = popupView.findViewById(R.id.btn2_finish);
        confirmWaterPlan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int water = Integer.parseInt(waterNum.getText().toString());
                String capacity = String.valueOf(water*250);
                waterSumView.setText("目标："+ capacity +"ml");
                mPopupWindow.dismiss();
            }
        });




        View popupView1 = getLayoutInflater().inflate(R.layout.popup_view1, null);

        mPopupWindow1 = new PopupWindow(popupView1, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        mPopupWindow1.setTouchable(true);
        mPopupWindow1.setOutsideTouchable(true);
        mPopupWindow1.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));

        Button btn2=inflate.findViewById(R.id.bn_4_button2);
        btn2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mPopupWindow1.showAtLocation(inflate.findViewById(R.id.bn_4_button2), Gravity.BOTTOM,0,0);
                mPopupWindow1.showAsDropDown(v);

            }
        });
        Button confirmExercisePlan = popupView1.findViewById(R.id.btn2_finish1);
        confirmExercisePlan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPopupWindow1.dismiss();
            }
        });



        mContext = getActivity();
        recyclerView = inflate.findViewById(R.id.rv_calendar);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(RecyclerView.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);
        days = new ArrayList<>();
        initData();

        adapter = new CalendarAdapter(days);
        recyclerView.setAdapter(adapter);


        return inflate;
    }




    private void initData() {
        String[] weekArr = {
                "Sun." , "Mon." , "Tues." , "Wed." , "Thur." , "Fir." , "Sat."
        };
        int j=0;
        for(int i=1; i <= 31; i++){

            Day day = new Day();
            day.setWeek(weekArr[j]);
            day.setDay(String.valueOf(i));
            days.add(day);
            j++;
            if(j >= 7){
                j=0;
            }
        }

    }



}